#ifndef __TMC5130_H
#define __TMC5130_H

#include "main.h"

/* TMC5130 GPIO引脚定义 */
#define TMC_CS_Pin          GPIO_PIN_12
#define TMC_CS_GPIO_Port    GPIOB
#define TMC_EN_Pin         GPIO_PIN_7
#define TMC_EN_GPIO_Port   GPIOA
#define TMC_MODE_Pin       GPIO_PIN_5
#define TMC_MODE_GPIO_Port GPIOA
#define TMC_SD_Pin        GPIO_PIN_6
#define TMC_SD_GPIO_Port  GPIOA

/* TMC5130寄存器地址定义 */
#define TMC5130_GCONF          0x00
#define TMC5130_GSTAT          0x01
#define TMC5130_IHOLD_IRUN     0x10
#define TMC5130_TPOWERDOWN     0x11
#define TMC5130_TSTEP          0x12
#define TMC5130_TPWMTHRS       0x13
#define TMC5130_TCOOLTHRS      0x14
#define TMC5130_THIGH          0x15
#define TMC5130_RAMPMODE       0x20
#define TMC5130_XACTUAL        0x21
#define TMC5130_VACTUAL        0x22
#define TMC5130_VSTART         0x23
#define TMC5130_A1             0x24
#define TMC5130_V1             0x25
#define TMC5130_AMAX           0x26
#define TMC5130_VMAX           0x27
#define TMC5130_DMAX           0x28
#define TMC5130_D1             0x2A
#define TMC5130_VSTOP          0x2B
#define TMC5130_TZEROWAIT      0x2C
#define TMC5130_XTARGET        0x2D
#define TMC5130_VDCMIN         0x33
#define TMC5130_SW_MODE        0x34
#define TMC5130_RAMP_STAT      0x35
#define TMC5130_ENCMODE        0x38
#define TMC5130_XENC           0x39
#define TMC5130_ENC_CONST      0x3A
#define TMC5130_ENC_STATUS     0x3B
#define TMC5130_ENC_LATCH      0x3C
#define TMC5130_CHOPCONF       0x6C
#define TMC5130_COOLCONF       0x6D
#define TMC5130_DCCTRL         0x6E
#define TMC5130_DRV_STATUS     0x6F
#define TMC5130_PWMCONF        0x70
#define TMC5130_PWM_SCALE      0x71
#define TMC5130_ENCM_CTRL      0x72
#define TMC5130_LOST_STEPS     0x73

/* 函数声明 */
void TMC5130_GPIO_Config(void);
void TMC5130_SPI_Write(uint8_t* buff, uint8_t size);
void TMC5130_SPI_Read(uint8_t* bufftx, uint8_t* buffrx, uint8_t size);
void TMC5130_WriteInt(int8_t address, int32_t value);
int32_t TMC5130_ReadInt(int8_t address);
void TMC5130_IHOLD_IRUN_Init(uint8_t ihold, uint8_t irun, uint8_t iholddelay);
void TMC5130_CHOPCONF_Init(uint8_t toff, uint8_t hstrt, uint8_t hend, uint8_t tbl, uint16_t md);

#endif /* __TMC5130_H */